using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Transforms;

namespace Game.Core
{
    public partial class UnitInfoSystem : SystemBase
    {
        protected override void OnCreate()
        {
            InitHealthInfoRereshQuery();
        }

        protected override void OnUpdate()
        {
            Dependency = ScheduleHealthInfoRereshJob(Dependency);
        }
        
        #region HealthInfo
        EntityQuery m_HealthInfoQuery;

        public void InitHealthInfoRereshQuery()
        {
            var builder = new EntityQueryBuilder(Allocator.Temp);
            builder.AddAll(ComponentType.ReadOnly<Parent>());
            builder.AddAll(ComponentType.ReadWrite<HealthPointRatio>());
            // builder.FinalizeQuery();
            m_HealthInfoQuery = GetEntityQuery(builder);
            builder.Dispose();
        }
        
        public JobHandle ScheduleHealthInfoRereshJob(JobHandle handle = new JobHandle())
        {
            var job = new HealthInfoRefreshJob();
            job.Damageables = GetComponentLookup<Damageable>(true);
            return job.ScheduleParallel(m_HealthInfoQuery, handle);
        }
        #endregion
    }
}